import java.sql.*;
import java.text.MessageFormat;


public class jdbcDemo1 {

    public static void main(String[] args) throws SQLException {

        Connection connection = null;
        Statement statement = null;
        ResultSet resultSet = null;

        try {
            //加载数据库厂商的驱动
            Class.forName("com.mysql.cj.jdbc.Driver");

            //获取数据库连接
            String url = "jdbc:mysql://127.0.0.1:3306/java113?characterEncoding=utf8&allowPublicKeyRetrieval=true&useSSL=false";
            String username = "root";
            String password = "gjm041027";
            connection = DriverManager.getConnection(url, username, password);

            //创建statement
            statement = connection.createStatement();

            //执行sql
            String sql = "select * from student";
            resultSet = statement.executeQuery(sql);

            //遍历结果集
            while (resultSet.next()) {
                Long student_id = resultSet.getLong("student_id");
                String sn = resultSet.getString("sn");
                String name = resultSet.getString("name");
                String mail = resultSet.getString("mail");
                Long class_id = resultSet.getLong("class_id");
//                System.out.println(MessageFormat.format("学生编号={0}, 学号={1}, 学生姓名={2}, 邮箱={3}, 班级编号={4}",
//                        student_id, sn, name, mail, class_id));
                //格式化输出
                System.out.println(MessageFormat.format("学生编号 = {0}，学号 = {1}，姓名 = {2}，邮箱 = {3}，班级编号 = {4}",
                        student_id, sn, name, mail, class_id));

            }

        } catch (ClassNotFoundException | SQLException e) {
            e.printStackTrace();
        } finally {
            //关闭资源
            if (resultSet != null) {
                resultSet.close();
            }
            if (statement != null) {
                statement.close();
            }
            if (connection != null) {
                connection.close();
            }
        }
    }

}
